package servicetestapp;


import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.Toolkit;
import java.io.*;
import java.net.*;

import crm.model.NetConst;
import crm.model.Setting;
import crm.model.studentPC.LockThread;
import crm.model.studentPC.win.WindowList;


import packages.ImagePackage;
import packages.MousePackage;
import packages.WindowNamePackage;


public class Main {
	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {
		new Main();
	}
	WindowList wList;
	Robot robot;
	LockThread lock ;

	public Main(){
		init();
		do {
			InetAddress ip = waitOnServer();
			if (ip == null) {
				System.exit(1);
			}
			Socket serverCon = connect(ip);
			start(serverCon);
		}while(true);
	}
	public void init(){
		wList = new WindowList();
		lock = new LockThread(wList);
		try {
			robot = new Robot();
		} catch (AWTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private InetAddress waitOnServer(){

		try {
			DatagramSocket  udpSocket = new DatagramSocket(Integer.parseInt(Setting.getString("Net.CommPort")));
			udpSocket.setBroadcast(true);
			byte[] buffer = new byte[0];
			DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
			System.out.println("new Connection");
			udpSocket.receive(packet);
			System.out.println("it has received");
			InetAddress sendeAdresse = packet.getAddress();
			udpSocket.close();
            return sendeAdresse;


		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;



	}

	private Socket connect(InetAddress ip){
		try {
			Socket s = new Socket(ip, 1515);
			return s;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}



	private void start(Socket s) {
		try{
			ObjectOutputStream os = new ObjectOutputStream(s.getOutputStream());
			ObjectInputStream is = new ObjectInputStream (s.getInputStream());
			while (!s.isClosed()){
				System.out.println("it is Bound");

				int request = (Integer)is.readObject();
				System.out.println("" + request);
				if (request == NetConst.CLOSE || request == -1){
					is.close();
					os.close();
					s.close();
				}else {
					if (request == NetConst.SCREENSHOT){
						os.writeObject(new ImagePackage());
						os.reset();
					}else if (request == NetConst.WINDOWS){
						wList.refresh();
						os.writeObject(new WindowNamePackage(wList));
						os.reset();
					}else if (request == NetConst.MOUSESET){
						MousePackage mp = (MousePackage)is.readObject();
						Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
						int x = (int)(mp.diffX * screenSize.width);
						int y = (int)(mp.diffY * screenSize.height);
						System.out.println(mp.diffX + "" + mp.diffY);
						robot.mouseMove(x, y);
					}else if (request == NetConst.LOGGEDUSER){
						os.writeObject(System.getProperty("user.name"));
					}else if (request == NetConst.SCREENLOCK){
						lock = new LockThread(wList);
						lock.lock();
					}else if (request == NetConst.SCREENUNLOCK){
						lock.unlock();
					}
				}
			}
		}catch(Exception e){

		}

	}



























}
